import json

from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt

from apps.mapper.sql_connect import add_user_data, verify_user_login, register_user, modify_user_data, \
    del_user_data, get_data_by_type


# Create your views here.
# 添加用户
@csrf_exempt
def add_user(request):
    username = request.POST.get('username')
    password = request.POST.get('password')
    type_ = request.POST.get('type')
    result = add_user_data(username, password, type_)
    if result:
        response = {
            "data": "添加成功"
        }
        return JsonResponse(response)
    else:
        response = {
            "data": "添加失败，用户名已存在。"
        }
        return JsonResponse(response)


# 登录逻辑
@csrf_exempt
def login(request):
    username = request.POST.get("username")
    password = request.POST.get("password")
    print(username, password)

    if password:
        # 登录校验
        result = verify_user_login(username, password)
        if result:
            code = 1
            message = f"欢迎您！用户{username}"

        else:
            code = 0
            message = "登录失败，请检查用户名和密码。"
    else:
        # 没有接收到数据
        code = 0
        message = "没有接收到数据，请检查数据是否填写完整"
    response = {
        'code': code,
        'message': message
    }
    return JsonResponse(response)


# 注册逻辑
@csrf_exempt
def register(request):
    username = request.POST.get("username")
    password = request.POST.get("password")
    if password:
        # 登录校验
        result = register_user(username, password)
        if result:
            code = 1
            message = f"注册成功,欢迎您，用户{username}"

        else:
            code = 0
            message = "注册失败，用户名已经存在"
    else:
        # 没有接收到数据
        code = 0
        message = "没有接收到数据"
    response = {
        'code': code,
        'message': message
    }
    return JsonResponse(response)


# 根据用户名修改用户信息
@csrf_exempt
def modify_user(request):
    user_name = request.POST.get("new_username")
    new_password = request.POST.get("new_password")
    new_type = request.POST.get("new_type")
    print(user_name, new_password, new_type)
    result = modify_user_data(user_name, new_password, new_type)
    if result:
        response = {
            "data": "修改成功"
        }
        return JsonResponse(response)
    else:
        response = {
            "data": "修改失败，请查看后端日志。"
        }
        return JsonResponse(response)


# 根据用户名删除用户信息
@csrf_exempt
def del_user(request):
    username = json.loads(request.body)['username']
    result = del_user_data(username)
    if result:
        response = {
            "data": "删除成功"
        }
        return JsonResponse(response)
    else:
        response = {
            "data": "删除失败，请查看后端日志。"
        }
        return JsonResponse(response)


# 根据数据类型获取数据
@csrf_exempt
def get_data(request):
    data_type = request.GET.get("data_type")
    result = get_data_by_type(data_type)
    columns = result.columns.tolist()
    result = [tuple(i) for _, i in result.iterrows()]
    if result:
        response = {
            "data": result,
            'columns':columns
        }
        return JsonResponse(response)
    else:
        response = {
            "data": "获取失败，请查看后端日志。"
        }
        return JsonResponse(response)
